/**
 *
 * @author
 *
 */
class SoundManager {
    private static lastBgMusicChannel: egret.SoundChannel;
    private static soundChannelMap:any={};
    private static bgSound: egret.Sound;
    private static bgName: string = "";
    private static isPlayingBgMuisc:boolean=false;
    public static restartBgMusic(): void{
        if(this.isPlayingBgMuisc){
            egret.setTimeout(()=>{
                this.bgName = null;
                this.isPlayingBgMuisc = false;
                this.playBgMusic();
            },this,100);
            
        }
    }
    public static pauseBgMusic(): void{
         var channel: egret.SoundChannel = SoundManager.lastBgMusicChannel;
        if (channel) {
            channel.stop();
        }
        this.bgName = "";
        SoundManager.lastBgMusicChannel = null;
    }
    /**
     * 播放背景音效
     * @param name 背景音效名称xxx_mp3格式，默认取GameConfig.music.bgMusic
     */
    public static playBgMusic(name: string = null): void {
        if (!name){
            name = GameConfig.music.bg;
        }
        if (name == this.bgName) return;
        if (!UserData.musicEnabled){
            SoundManager.stopBgMusic();
            return;
        }
            
        if(this.isPlayingBgMuisc)return;
        egret.Tween.get(this).wait(100).call(() => {
            this.bgName = name;
            var sound: egret.Sound = this.bgSound = RES.getRes(name);
            if (sound) {
                this.onLoad(sound);
            } else {
                RES.getResAsync(name, this.onLoad, this);
            }
        });
    }
    private static onLoad(sound: egret.Sound) {
        sound.type = egret.Sound.MUSIC;
        var channel: egret.SoundChannel = SoundManager.lastBgMusicChannel;
        if (channel) {
            channel.stop();
        }
        this.isPlayingBgMuisc = true;
        SoundManager.lastBgMusicChannel = sound.play(0, -1);
    }
    public static stopBgMusic(): void {
        var channel: egret.SoundChannel = SoundManager.lastBgMusicChannel;
        if (channel) {
            channel.stop();
        }
        this.isPlayingBgMuisc = false;
        this.bgName = "";
        SoundManager.lastBgMusicChannel = null;
    }

    /**
     * 播放特殊音效
     */
    public static playEffect(name: string = null): void {
        if (!UserData.musicEnabled || !name){
            return;
        }
        function onLoad(sound: egret.Sound,name:string,) {
            try {
                let len = sound.length;
                let channel: egret.SoundChannel = sound.play(0, 1);
                SoundManager.soundChannelMap[name] = channel;
                egret.setTimeout(()=>{
                    SoundManager.stopEffect(name);
                },this,len*1000);
            } catch (e) { };
        }
        var sound: egret.Sound = RES.getRes(name);
        SoundManager.stopEffect(name);
        if (sound) {
            onLoad(sound,name);
        } else {
            RES.getResAsync(name, onLoad, this);
        }
    }
    public static stopEffect(name:string = null): void{
        let channel: egret.SoundChannel = SoundManager.soundChannelMap[name];
        if(channel){
            channel.stop();
            SoundManager.soundChannelMap[name] = null;
        }
    }
}
